import 'package:flutter/material.dart';

///返回按键，默认样式
class GoBackButton extends StatelessWidget {
  final Widget? widget;
  final String? icon;
  final Color? iconColor;
  final VoidCallback? onPressed;
  final bool touchLimit; //是否点击限制
  final int touchLimitSecond; //时长

  GoBackButton({
    Key? key,
    this.widget,
    this.icon,
    this.iconColor,
    this.onPressed,
    this.touchLimit = true,
    this.touchLimitSecond = 1,
  }) : super(key: key);

  bool _isTap = false;

  @override
  Widget build(BuildContext context) {
    return IconButton(
      icon: widget ??
          (icon?.isNotEmpty == true
              ? Image.asset(
                  icon!,
                  color: iconColor, // ?? Colors.white,
                )
              : Icon(
                  Icons.arrow_back_ios_new,
                  color: iconColor,
                )),
      color: iconColor, // ?? Colors.white,
      // tooltip: MaterialLocalizations.of(context).backButtonTooltip,
      onPressed: () {
        //1秒点一次
        if (_isTap == true && touchLimit == true) {
          return;
        }
        _isTap = true;
        FocusScope.of(context).unfocus();
        if (onPressed != null) {
          onPressed!();
        } else {
          Navigator.maybePop(context);
        }
        Future.delayed(Duration(seconds: touchLimitSecond)).then((_) {
          _isTap = false;
        });
      },
    );
  }
}
